#region

using System.Data.Entity;

#endregion

namespace InfoTech.VinhSon.Bussiness
{
    public class VinhSonContext : DbContext
    {
        public VinhSonContext() : base("VinhSondb")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // Table: User
            modelBuilder.Entity<User>()
                        .ToTable("User",
                                 "dbo");
            modelBuilder.Entity<User>()
                        .HasKey(c => new
                                         {
                                                 c.Id
                                         });
            // Table: Article
            modelBuilder.Entity<Article>()
                        .ToTable("Article",
                                 "dbo");
            modelBuilder.Entity<Article>()
                        .HasKey(c => new
                                         {
                                                 c.Id
                                         });
            modelBuilder.Entity<Article>()
                        .HasMany(c => c.Sections)
                        .WithOptional(c => c.Article)
                        .Map(c => c.MapKey("ArticleId"));
            // Table: Category
            modelBuilder.Entity<Category>()
                        .ToTable("Category",
                                 "dbo");
            modelBuilder.Entity<Category>()
                        .HasKey(c => new
                                         {
                                                 c.Id
                                         });
            modelBuilder.Entity<Category>()
                        .HasMany(c => c.Articles)
                        .WithOptional(c => c.Category)
                        .Map(c => c.MapKey("CategoryId"));
            modelBuilder.Entity<Category>()
                        .HasMany(c => c.Categories)
                        .WithOptional(c => c.Parent)
                        .Map(c => c.MapKey("ParentId"));
            modelBuilder.Entity<Category>()
                        .HasMany(c => c.CSections)
                        .WithOptional(c => c.Category)
                        .Map(c => c.MapKey("CategoryId"));
            // Table: Section
            modelBuilder.Entity<Section>()
                        .ToTable("Section",
                                 "dbo");
            modelBuilder.Entity<Section>()
                        .HasKey(c => new
                                         {
                                                 c.Id
                                         });
            // Table: Config
            modelBuilder.Entity<Config>()
                        .ToTable("Config",
                                 "dbo");
            modelBuilder.Entity<Config>()
                        .HasKey(c => new
                                         {
                                                 c.Id
                                         });
            // Table: CSection
            modelBuilder.Entity<CSection>()
                        .ToTable("CSection",
                                 "dbo");
            modelBuilder.Entity<CSection>()
                        .HasKey(c => new
                                         {
                                                 c.Id
                                         });
            // Table: Layout
            modelBuilder.Entity<Layout>()
                        .ToTable("Layout",
                                 "dbo");
            modelBuilder.Entity<Layout>()
                        .HasKey(c => new
                                         {
                                                 c.Id
                                         });
            modelBuilder.Entity<Layout>()
                        .HasMany(c => c.Groups)
                        .WithOptional(c => c.Layout)
                        .Map(c => c.MapKey("LayoutId"));
            // Table: Group
            modelBuilder.Entity<Group>()
                        .ToTable("Group",
                                 "dbo");
            modelBuilder.Entity<Group>()
                        .HasKey(c => new
                                         {
                                                 c.Id
                                         });
            modelBuilder.Entity<Group>()
                        .HasMany(c => c.Articles)
                        .WithMany(c => c.Groups)
                        .Map(c =>
                                 {
                                     c.MapLeftKey("GroupId");
                                     c.MapRightKey("ArticleId");
                                     c.ToTable("GroupArticle");
                                 });
            // Table: ArticleGroup
            modelBuilder.Entity<ArticleGroup>().ToTable("ArticleGroup", "dbo");
            modelBuilder.Entity<ArticleGroup>().HasKey(c => new { c.Id });
            modelBuilder.Entity<Article>().HasMany(c => c.ArticleGroups).WithOptional(c => c.Article).Map(c => c.MapKey("ArticleId"));
            modelBuilder.Entity<Group>().HasMany(c => c.ArticleGroups).WithOptional(c => c.Group).Map(c => c.MapKey("GroupId"));
        }
    }
}
